package jnpf.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jnpf.base.UserInfo;
import jnpf.config.PermissionsConfig;
import jnpf.entity.ZXGL_RenWuBianGengEntity;
import jnpf.entity.ZXGL_RenWuFanKuiEntity;
import jnpf.entity.ZXGL_XiangMuFenJie_mingxibiaoEntity;
import jnpf.entity.ZXGL_XiangMuLiXiang_MingXiEntity;
import jnpf.mapper.ZXGL_XiangMuFenJie_mingxibiaoMapper;
import jnpf.model.zxgl_xiangmufenjie_mingxibiao.ZXGL_XiangMuFenJie_mingxibiaoPagination;
import jnpf.permission.service.AuthorizeService;
import jnpf.service.ZXGL_XiangMuFenJie_mingxibiaoService;
import jnpf.service.ZXGL_XiangMuLiXiang_MingXiService;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ZXGL_XiangMuFenJie_mingxibiao
 * 版本： V2.0
 * 版权： 河北交投智能交通技术有限责任公司
 * 作者： 数字交投项目组
 * 日期： 2025-03-20
 */
@Service
public class ZXGL_XiangMuFenJie_mingxibiaoServiceImpl extends ServiceImpl<ZXGL_XiangMuFenJie_mingxibiaoMapper, ZXGL_XiangMuFenJie_mingxibiaoEntity> implements ZXGL_XiangMuFenJie_mingxibiaoService {


    @Autowired
    private UserProvider userProvider;
    //@Autowired
    //private OrganizeApi organizeApi;
    @Resource
    PermissionsConfig permissionsConfig;
    @Autowired
    private AuthorizeService authorizeService;

    @Autowired
    private ZXGL_XiangMuLiXiang_MingXiService zxgl_xiangMuLiXiang_mingXiService;


    @Override
    public List<ZXGL_XiangMuFenJie_mingxibiaoEntity> getList(ZXGL_XiangMuFenJie_mingxibiaoPagination zXGL_XiangMuFenJie_mingxibiaoPagination) {
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> zXGL_XiangMuFenJie_mingxibiaoQueryWrapper = new QueryWrapper<>();
        //todo 增加判断是不是来源于项目库的任务记录
        //TODO 增加权限逻辑
        UserInfo userInfo = userProvider.get();
        if (!userInfo.getIsAdministrator() && !"tasktab".equals(zXGL_XiangMuFenJie_mingxibiaoPagination.getFrom())) {
            String alldataRoleId = permissionsConfig.getAlldataRoleid();
            List<String> roleIds = userInfo.getRoleIds();
            if (roleIds == null || (StringUtils.isNotBlank(alldataRoleId) && !roleIds.contains(alldataRoleId))) {
                String userId = userInfo.getUserId();
                zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().and(wrapper -> wrapper
                        .eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getCreatorUserId, userId)//创建人看本人数据
                        .or()
                        .eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getZrr, userId)//主责人看自己负责项目数据
                        .or()
                        .eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getDepartmentManagerZzbm, userId)//主责部门负责人看本部门负责项目数据
                        .or()
                        .eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getDepartmentHeadleaderZzbm, userId)//主责部门主管领导看所管理部门负责数据
                        .or()
                        .like(ZXGL_XiangMuFenJie_mingxibiaoEntity::getXzr, userId)//协作人看自己协作项目数据
                );
            }
        }
        int zXGL_XiangMuFenJie_mingxibiaoNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuFenJie_mingxibiaoPagination.getRwmc())) {
            zXGL_XiangMuFenJie_mingxibiaoNum++;
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().like(ZXGL_XiangMuFenJie_mingxibiaoEntity::getRwmc, zXGL_XiangMuFenJie_mingxibiaoPagination.getRwmc());
        }
        //查询单条数据
        if (StringUtil.isNotEmpty(zXGL_XiangMuFenJie_mingxibiaoPagination.getXmid())) {
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getXmid, zXGL_XiangMuFenJie_mingxibiaoPagination.getXmid());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuFenJie_mingxibiaoPagination.getZrr())) {
            zXGL_XiangMuFenJie_mingxibiaoNum++;
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getZrr, zXGL_XiangMuFenJie_mingxibiaoPagination.getZrr());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuFenJie_mingxibiaoPagination.getZrbm())) {
            zXGL_XiangMuFenJie_mingxibiaoNum++;
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getZrbm, zXGL_XiangMuFenJie_mingxibiaoPagination.getZrbm());
        }

        if (AllIdList.size() > 0) {
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().in(ZXGL_XiangMuFenJie_mingxibiaoEntity::getId, AllIdList);
        }


        /**
         * 数据权限 此处手动写入菜单id
         */
//        String moduleId = "672362412510312069";
//        if (!userProvider.get().getIsAdministrator()) {
//            Object conditionObj = authorizeService.getCondition(zXGL_XiangMuFenJie_mingxibiaoQueryWrapper, userProvider.get(), moduleId);
//            if (ObjectUtil.isEmpty(conditionObj)) {
//                return new ArrayList<>();
//            }
//            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper = (QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity>) authorizeService.getCondition(zXGL_XiangMuFenJie_mingxibiaoQueryWrapper, userProvider.get(), moduleId);
//        }

        //排序
//        if (StringUtil.isEmpty(zXGL_XiangMuFenJie_mingxibiaoPagination.getSidx())) {
//            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuFenJie_mingxibiaoEntity::getJhksrq);
//        } else {
//            try {
//                String sidx = zXGL_XiangMuFenJie_mingxibiaoPagination.getSidx();
//                ZXGL_XiangMuFenJie_mingxibiaoEntity zXGL_XiangMuFenJie_mingxibiaoEntity = new ZXGL_XiangMuFenJie_mingxibiaoEntity();
//                if(sidx.toLowerCase().equals("creatortime")){
//                    sidx = "creatorTime";
//                }
//                Field declaredField = zXGL_XiangMuFenJie_mingxibiaoEntity.getClass().getDeclaredField(sidx);
//                declaredField.setAccessible(true);
//                String value = declaredField.getAnnotation(TableField.class).value();
//                zXGL_XiangMuFenJie_mingxibiaoQueryWrapper = "asc".equals(zXGL_XiangMuFenJie_mingxibiaoPagination.getSort().toLowerCase()) ? zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.orderByAsc(value) : zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.orderByDesc(value);
//            } catch (NoSuchFieldException e) {
//                e.printStackTrace();
//            }
//        }

        zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuFenJie_mingxibiaoEntity::getCreatorTime);
        if ((total > 0 && AllIdList.size() > 0) || total == 0) {
            Page<ZXGL_XiangMuFenJie_mingxibiaoEntity> page = new Page<>(zXGL_XiangMuFenJie_mingxibiaoPagination.getCurrentPage(), zXGL_XiangMuFenJie_mingxibiaoPagination.getPageSize());
            IPage<ZXGL_XiangMuFenJie_mingxibiaoEntity> userIPage = this.page(page, zXGL_XiangMuFenJie_mingxibiaoQueryWrapper);
            return zXGL_XiangMuFenJie_mingxibiaoPagination.setData(userIPage.getRecords(), userIPage.getTotal());
        } else {
            List<ZXGL_XiangMuFenJie_mingxibiaoEntity> list = new ArrayList();
            return zXGL_XiangMuFenJie_mingxibiaoPagination.setData(list, list.size());
        }
    }

    @Override
    public List<ZXGL_XiangMuFenJie_mingxibiaoEntity> getTypeList(ZXGL_XiangMuFenJie_mingxibiaoPagination zXGL_XiangMuFenJie_mingxibiaoPagination, String dataType) {
        String userId = userProvider.get().getUserId();
        List<String> AllIdList = new ArrayList();
        int total = 0;
        QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> zXGL_XiangMuFenJie_mingxibiaoQueryWrapper = new QueryWrapper<>();
        int zXGL_XiangMuFenJie_mingxibiaoNum = 0;
        if (StringUtil.isNotEmpty(zXGL_XiangMuFenJie_mingxibiaoPagination.getRwmc())) {
            zXGL_XiangMuFenJie_mingxibiaoNum++;
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().like(ZXGL_XiangMuFenJie_mingxibiaoEntity::getRwmc, zXGL_XiangMuFenJie_mingxibiaoPagination.getRwmc());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuFenJie_mingxibiaoPagination.getZrr())) {
            zXGL_XiangMuFenJie_mingxibiaoNum++;
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getZrr, zXGL_XiangMuFenJie_mingxibiaoPagination.getZrr());
        }

        if (StringUtil.isNotEmpty(zXGL_XiangMuFenJie_mingxibiaoPagination.getZrbm())) {
            zXGL_XiangMuFenJie_mingxibiaoNum++;
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getZrbm, zXGL_XiangMuFenJie_mingxibiaoPagination.getZrbm());
        }

        if (AllIdList.size() > 0) {
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().in(ZXGL_XiangMuFenJie_mingxibiaoEntity::getId, AllIdList);
        }
        //排序
        if (StringUtil.isEmpty(zXGL_XiangMuFenJie_mingxibiaoPagination.getSidx())) {
            zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().orderByDesc(ZXGL_XiangMuFenJie_mingxibiaoEntity::getId);
        } else {
            try {
                String sidx = zXGL_XiangMuFenJie_mingxibiaoPagination.getSidx();
                ZXGL_XiangMuFenJie_mingxibiaoEntity zXGL_XiangMuFenJie_mingxibiaoEntity = new ZXGL_XiangMuFenJie_mingxibiaoEntity();
                Field declaredField = zXGL_XiangMuFenJie_mingxibiaoEntity.getClass().getDeclaredField(sidx);
                declaredField.setAccessible(true);
                String value = declaredField.getAnnotation(TableField.class).value();
                zXGL_XiangMuFenJie_mingxibiaoQueryWrapper = "asc".equals(zXGL_XiangMuFenJie_mingxibiaoPagination.getSort().toLowerCase()) ? zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.orderByAsc(value) : zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.orderByDesc(value);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
        }
        if ("0".equals(dataType)) {
            if ((total > 0 && AllIdList.size() > 0) || total == 0) {
                Page<ZXGL_XiangMuFenJie_mingxibiaoEntity> page = new Page<>(zXGL_XiangMuFenJie_mingxibiaoPagination.getCurrentPage(), zXGL_XiangMuFenJie_mingxibiaoPagination.getPageSize());
                IPage<ZXGL_XiangMuFenJie_mingxibiaoEntity> userIPage = this.page(page, zXGL_XiangMuFenJie_mingxibiaoQueryWrapper);
                return zXGL_XiangMuFenJie_mingxibiaoPagination.setData(userIPage.getRecords(), userIPage.getTotal());
            } else {
                List<ZXGL_XiangMuFenJie_mingxibiaoEntity> list = new ArrayList();
                return zXGL_XiangMuFenJie_mingxibiaoPagination.setData(list, list.size());
            }
        } else {
            return this.list(zXGL_XiangMuFenJie_mingxibiaoQueryWrapper);
        }
    }

    @Override
    public ZXGL_XiangMuFenJie_mingxibiaoEntity getInfo(String id) {
        QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void create(ZXGL_XiangMuFenJie_mingxibiaoEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, ZXGL_XiangMuFenJie_mingxibiaoEntity entity) {
        entity.setId(id);
        return this.updateById(entity);
    }

    @Override
    public void delete(ZXGL_XiangMuFenJie_mingxibiaoEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法


    @Override
    public ZXGL_RenWuFanKuiEntity getMingxibiao(String Id) {
        UserInfo user = userProvider.get();
        ZXGL_XiangMuFenJie_mingxibiaoEntity entity = baseMapper.selectById(Id);
        if (entity != null) {


            ZXGL_RenWuFanKuiEntity fanKui = new ZXGL_RenWuFanKuiEntity();
            fanKui.setFkr(user.getUserId());
            fanKui.setFkbm(user.getDepartmentId());
            fanKui.setFkrq(new DateTime());
            fanKui.setSsxm(entity.getSsxm());
            if (StringUtil.isNotEmpty(entity.getXmid())) {
                ZXGL_XiangMuLiXiang_MingXiEntity model = zxgl_xiangMuLiXiang_mingXiService.getById(entity.getXmid());
                fanKui.setXmbh(model.getXmbh());
            }

            fanKui.setXmid(entity.getXmid());
            fanKui.setRwmc(entity.getRwmc());
            fanKui.setRwid(entity.getId());
            fanKui.setRwbh(entity.getRwbh());
            fanKui.setRwlx(entity.getRwlx());
            // fanKui.setJhmbz(entity.getMbwcz());
            // fanKui.setMbwcz(entity.getMbwcz());
            fanKui.setMbwcz(entity.getSjz());
            if (entity.getRwqz() != null) {
                fanKui.setRwqz(entity.getRwqz());
            }
            if (StringUtil.isNotEmpty(entity.getRwjd())) {
                fanKui.setDqrwjd(new BigDecimal(entity.getRwjd()));
            }
            fanKui.setXbr(entity.getXzr());
            fanKui.setMbwcz(entity.getSjz());
            fanKui.setMblx(entity.getSjlx());
            fanKui.setMbzdw(entity.getSjdw());
            //协办人是否已全部反馈
            return fanKui;
        }
        return null;
    }

    @Override
    public List<ZXGL_XiangMuFenJie_mingxibiaoEntity> getTodayList() {
        QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> zXGL_XiangMuLiXiang_MingXiQueryWrapper = new QueryWrapper<>();
        zXGL_XiangMuLiXiang_MingXiQueryWrapper.lambda().le(ZXGL_XiangMuFenJie_mingxibiaoEntity::getJhksrq, LocalDate.now());
        zXGL_XiangMuLiXiang_MingXiQueryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getZt, "667276252607944005")
                .or().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getZt, "")
                .or().isNull(ZXGL_XiangMuFenJie_mingxibiaoEntity::getZt);
        return baseMapper.selectList(zXGL_XiangMuLiXiang_MingXiQueryWrapper);
    }

    @Override
    public List<ZXGL_XiangMuFenJie_mingxibiaoEntity> getList(String xmId) {
        QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> zXGL_XiangMuFenJie_mingxibiaoQueryWrapper = new QueryWrapper<>();
        zXGL_XiangMuFenJie_mingxibiaoQueryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getXmid, xmId);
        return this.list(zXGL_XiangMuFenJie_mingxibiaoQueryWrapper);
    }

    /***
     * 任务变更带出对应信息
     * @param id
     * @return
     */
    @Override
    public ZXGL_RenWuBianGengEntity getRenWuBianGengMX(String id) {
        UserInfo user = userProvider.get();
        ZXGL_XiangMuFenJie_mingxibiaoEntity entity = baseMapper.selectById(id);
        ZXGL_RenWuBianGengEntity renWuBianGeng = new ZXGL_RenWuBianGengEntity();
        if (ObjectUtil.isNotEmpty(entity)) {
            renWuBianGeng.setSqr(user.getUserId());
            renWuBianGeng.setSqbm(user.getDepartmentId());
            renWuBianGeng.setSqrq(new DateTime());
            renWuBianGeng.setXmrwlx(entity.getRwlx());
            renWuBianGeng.setXmid(entity.getXmid());
            renWuBianGeng.setRwmc(entity.getRwmc());
            renWuBianGeng.setRwid(entity.getId());
            renWuBianGeng.setBgqrwzt(entity.getZt());
            renWuBianGeng.setBgqjhksrq(entity.getJhksrq());
            renWuBianGeng.setBgqrwmc(entity.getRwmc());
            renWuBianGeng.setBgqjhjsrq(entity.getJhwcrq());
            renWuBianGeng.setBiangengqiangongzuomubiao(entity.getGzmb());
            renWuBianGeng.setBgqjhz(entity.getSjz());
        }
        return renWuBianGeng;
    }

    @Override
    public List<String> getListTree() {
        QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> zxglXiangMuFenJieMingxibiaoEntityQueryWrapper = new QueryWrapper<>();
        List<ZXGL_XiangMuFenJie_mingxibiaoEntity> zxglXiangMuFenJieMingxibiaoEntities = baseMapper.selectList(zxglXiangMuFenJieMingxibiaoEntityQueryWrapper);
        List<String> collect = zxglXiangMuFenJieMingxibiaoEntities.stream()
                .map(ZXGL_XiangMuFenJie_mingxibiaoEntity::getXmid)
                .collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<ZXGL_XiangMuFenJie_mingxibiaoEntity> getListByXmId(String xmid) {
        QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getXmid, xmid);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<ZXGL_XiangMuFenJie_mingxibiaoEntity> getUnfinishedBefore5Day() {
        QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> zXGL_XiangMuLiXiang_MingXiQueryWrapper = new QueryWrapper<>();
        LocalDate date = LocalDate.now();
        date = date.plusDays(5);
        zXGL_XiangMuLiXiang_MingXiQueryWrapper.lambda().le(ZXGL_XiangMuFenJie_mingxibiaoEntity::getJhwcrq, date);
        zXGL_XiangMuLiXiang_MingXiQueryWrapper.lambda().ne(ZXGL_XiangMuFenJie_mingxibiaoEntity::getRwjd, "100")
                .or().isNull(ZXGL_XiangMuFenJie_mingxibiaoEntity::getRwjd)
                .or().eq(ZXGL_XiangMuFenJie_mingxibiaoEntity::getRwjd, "");
        return baseMapper.selectList(zXGL_XiangMuLiXiang_MingXiQueryWrapper);
    }

    @Override
    public BigDecimal selectXmjd(String xmId) {
        return baseMapper.selectXmjd(xmId);
    }

    @Override
    public String findMaxSequence(String xmid, String prefix) {
        return baseMapper.findMaxSequence(xmid, prefix);
    }

    @Override
    public ZXGL_RenWuFanKuiEntity createEntity(String Id) {
        ZXGL_XiangMuFenJie_mingxibiaoEntity entity = baseMapper.selectById(Id);
        if (entity != null) {
            ZXGL_RenWuFanKuiEntity model = new ZXGL_RenWuFanKuiEntity();
            model.setFkr(entity.getZrr());
            model.setFkbm(entity.getZrbm());
            model.setFkrq(new DateTime());
            model.setSsxm(entity.getSsxm());
            if (StringUtil.isNotEmpty(entity.getXmid())) {
                ZXGL_XiangMuLiXiang_MingXiEntity mingXiEntity = zxgl_xiangMuLiXiang_mingXiService.getById(entity.getXmid());
                model.setXmbh(mingXiEntity.getXmbh());
            }

            model.setXmid(entity.getXmid());
            model.setRwmc(entity.getRwmc());
            model.setRwid(entity.getId());
            model.setRwbh(entity.getRwbh());
            model.setRwlx(entity.getRwlx());
            model.setMbwcz(entity.getSjz());
            if (entity.getRwqz() != null) {
                model.setRwqz(entity.getRwqz());
            }
            if (StringUtil.isNotEmpty(entity.getRwjd())) {
                model.setDqrwjd(new BigDecimal(entity.getRwjd()));
            }
            model.setXbr(entity.getXzr());
            model.setMbwcz(entity.getSjz());
            model.setMblx(entity.getSjlx());
            model.setMbzdw(entity.getSjdw());
            //协办人是否已全部反馈
            return model;
        }
        return null;
    }

    @Override
    public List<ZXGL_XiangMuFenJie_mingxibiaoEntity> getOverdueList(List<String> yjbjs) {
        QueryWrapper<ZXGL_XiangMuFenJie_mingxibiaoEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(ZXGL_XiangMuFenJie_mingxibiaoEntity::getYjbj, yjbjs);
        return baseMapper.selectList(queryWrapper);
    }
}
